<template>
  qeqweq
  <a-input v-model:value="address" placeholder="ws地址" />
  <a-button @click="open">连接</a-button>
</template>
<script>
export default {
  name: 'Websocket1',
};
</script>
<script setup>
import { onMounted, ref } from 'vue';
import { useMessage } from '@/hooks/web/useMessage';
import { getToken } from '@/utils/auth';
const address = ref(`ws://localhost:8099/api/ws/notice?token=Bearer ${getToken()}`);
const { createMessage } = useMessage();
// websocket
const ws = ref(null);
const message = ref('');
const open = () => {
  ws.value = new WebSocket(address.value);
  ws.value.onopen = () => {
    console.log('open');
  };
  ws.value.onmessage = (e) => {
    message.value = e.data;
    console.log(e.data);
  };
  ws.value.onclose = (e) => {
    console.log('close', e);
  };
  ws.value.onerror = (e) => {
    createMessage.success(e);
  };
};
onMounted(() => {
  open();
});
</script>
